package com.zhaodui.pro.service.impl;
import com.zhaodui.pro.service.FxjProMainServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.zhaodui.pro.entity.FxjProMainEntity;
import com.zhaodui.pro.page.FxjProMainPage;
import com.zhaodui.pro.entity.FxjProAttEntity;
import com.zhaodui.pro.entity.FxjProContractEntity;
import com.zhaodui.pro.entity.FxjProGpsEntity;
import com.zhaodui.pro.entity.FxjProCostEntity;
import com.zhaodui.pro.entity.FxjProCostCountEntity;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;
import java.util.ArrayList;
import java.util.UUID;
import java.io.Serializable;

import java.util.Map;
import java.util.HashMap;
import org.jeecgframework.minidao.util.FreemarkerParseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.jeecgframework.core.util.ResourceUtil;

@Service("fxjProMainService")
@Transactional
public class FxjProMainServiceImpl extends CommonServiceImpl implements FxjProMainServiceI {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
 	public void delete(FxjProMainEntity entity) throws Exception{
 		super.delete(entity);
 	}
	
	public void addMain(FxjProMainPage fxjProMainPage) throws Exception {
		FxjProMainEntity fxjProMain = new FxjProMainEntity();
		MyBeanUtils.copyBeanNotNull2Bean(fxjProMainPage, fxjProMain);
		//保存主信息
		this.save(fxjProMain);
		/**保存-附件*/
		List<FxjProAttEntity> fxjProAttList = fxjProMainPage.getFxjProAttList();
		for(FxjProAttEntity fxjProAtt:fxjProAttList){
			//外键设置
			fxjProAtt.setProNo(fxjProMain.getProNo());
			this.save(fxjProAtt);
		}
		/**保存-合同*/
		List<FxjProContractEntity> fxjProContractList = fxjProMainPage.getFxjProContractList();
		for(FxjProContractEntity fxjProContract:fxjProContractList){
			//外键设置
			fxjProContract.setProNo(fxjProMain.getProNo());
			this.save(fxjProContract);
		}
		/**保存-GPS*/
		List<FxjProGpsEntity> fxjProGpsList = fxjProMainPage.getFxjProGpsList();
		for(FxjProGpsEntity fxjProGps:fxjProGpsList){
			//外键设置
			fxjProGps.setProNo(fxjProMain.getProNo());
			this.save(fxjProGps);
		}
		/**保存-费用*/
		List<FxjProCostEntity> fxjProCostList = fxjProMainPage.getFxjProCostList();
		for(FxjProCostEntity fxjProCost:fxjProCostList){
			//外键设置
			fxjProCost.setProNo(fxjProMain.getProNo());
			this.save(fxjProCost);
		}
		/**保存-其他计算*/
		List<FxjProCostCountEntity> fxjProCostCountList = fxjProMainPage.getFxjProCostCountList();
		for(FxjProCostCountEntity fxjProCostCount:fxjProCostCountList){
			//外键设置
			fxjProCostCount.setProNo(fxjProMain.getProNo());
			this.save(fxjProCostCount);
		}
	}

	
	public void updateMain(FxjProMainPage fxjProMainPage) throws Exception{
		FxjProMainEntity fxjProMain = new FxjProMainEntity();
		//保存主表信息
		if(StringUtil.isNotEmpty(fxjProMainPage.getId())){
			fxjProMain = findUniqueByProperty(FxjProMainEntity.class, "id", fxjProMainPage.getId());
			MyBeanUtils.copyBeanNotNull2Bean(fxjProMainPage, fxjProMain);
			this.saveOrUpdate(fxjProMain);
		}else{
			this.saveOrUpdate(fxjProMain);
		}
		//===================================================================================
		//获取参数
		Object proNo0 = fxjProMain.getProNo();
		Object proNo1 = fxjProMain.getProNo();
		Object proNo2 = fxjProMain.getProNo();
		Object proNo3 = fxjProMain.getProNo();
		Object proNo4 = fxjProMain.getProNo();
		//===================================================================================
		//1.附件数据库的数据
	    String hql0 = "from FxjProAttEntity where 1 = 1 AND proNo = ? ";
	    List<FxjProAttEntity> fxjProAttOldList = this.findHql(hql0,proNo0);
		//2.附件新的数据
		List<FxjProAttEntity> fxjProAttList = fxjProMainPage.getFxjProAttList();
	    //3.筛选更新明细数据-附件
		if(fxjProAttList!=null &&fxjProAttList.size()>0){
			for(FxjProAttEntity oldE:fxjProAttOldList){
				boolean isUpdate = false;
				for(FxjProAttEntity sendE:fxjProAttList){
					//需要更新的明细数据-附件
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-附件
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-附件
			for(FxjProAttEntity fxjProAtt:fxjProAttList){
				if(oConvertUtils.isEmpty(fxjProAtt.getId())){
					//外键设置
					fxjProAtt.setProNo(fxjProMain.getProNo());
					this.save(fxjProAtt);
				}
			}
		}
		//===================================================================================
		//1.合同数据库的数据
	    String hql1 = "from FxjProContractEntity where 1 = 1 AND proNo = ? ";
	    List<FxjProContractEntity> fxjProContractOldList = this.findHql(hql1,proNo1);
		//2.合同新的数据
		List<FxjProContractEntity> fxjProContractList = fxjProMainPage.getFxjProContractList();
	    //3.筛选更新明细数据-合同
		if(fxjProContractList!=null &&fxjProContractList.size()>0){
			for(FxjProContractEntity oldE:fxjProContractOldList){
				boolean isUpdate = false;
				for(FxjProContractEntity sendE:fxjProContractList){
					//需要更新的明细数据-合同
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-合同
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-合同
			for(FxjProContractEntity fxjProContract:fxjProContractList){
				if(oConvertUtils.isEmpty(fxjProContract.getId())){
					//外键设置
					fxjProContract.setProNo(fxjProMain.getProNo());
					this.save(fxjProContract);
				}
			}
		}
		//===================================================================================
		//1.GPS数据库的数据
	    String hql2 = "from FxjProGpsEntity where 1 = 1 AND proNo = ? ";
	    List<FxjProGpsEntity> fxjProGpsOldList = this.findHql(hql2,proNo2);
		//2.GPS新的数据
		List<FxjProGpsEntity> fxjProGpsList = fxjProMainPage.getFxjProGpsList();
	    //3.筛选更新明细数据-GPS
		if(fxjProGpsList!=null &&fxjProGpsList.size()>0){
			for(FxjProGpsEntity oldE:fxjProGpsOldList){
				boolean isUpdate = false;
				for(FxjProGpsEntity sendE:fxjProGpsList){
					//需要更新的明细数据-GPS
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-GPS
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-GPS
			for(FxjProGpsEntity fxjProGps:fxjProGpsList){
				if(oConvertUtils.isEmpty(fxjProGps.getId())){
					//外键设置
					fxjProGps.setProNo(fxjProMain.getProNo());
					this.save(fxjProGps);
				}
			}
		}
		//===================================================================================
		//1.费用数据库的数据
	    String hql3 = "from FxjProCostEntity where 1 = 1 AND proNo = ? ";
	    List<FxjProCostEntity> fxjProCostOldList = this.findHql(hql3,proNo3);
		//2.费用新的数据
		List<FxjProCostEntity> fxjProCostList = fxjProMainPage.getFxjProCostList();
	    //3.筛选更新明细数据-费用
		if(fxjProCostList!=null &&fxjProCostList.size()>0){
			for(FxjProCostEntity oldE:fxjProCostOldList){
				boolean isUpdate = false;
				for(FxjProCostEntity sendE:fxjProCostList){
					//需要更新的明细数据-费用
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-费用
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-费用
			for(FxjProCostEntity fxjProCost:fxjProCostList){
				if(oConvertUtils.isEmpty(fxjProCost.getId())){
					//外键设置
					fxjProCost.setProNo(fxjProMain.getProNo());
					this.save(fxjProCost);
				}
			}
		}
		//===================================================================================
		//1.其他计算数据库的数据
	    String hql4 = "from FxjProCostCountEntity where 1 = 1 AND proNo = ? ";
	    List<FxjProCostCountEntity> fxjProCostCountOldList = this.findHql(hql4,proNo4);
		//2.其他计算新的数据
		List<FxjProCostCountEntity> fxjProCostCountList = fxjProMainPage.getFxjProCostCountList();
	    //3.筛选更新明细数据-其他计算
		if(fxjProCostCountList!=null &&fxjProCostCountList.size()>0){
			for(FxjProCostCountEntity oldE:fxjProCostCountOldList){
				boolean isUpdate = false;
				for(FxjProCostCountEntity sendE:fxjProCostCountList){
					//需要更新的明细数据-其他计算
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-其他计算
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-其他计算
			for(FxjProCostCountEntity fxjProCostCount:fxjProCostCountList){
				if(oConvertUtils.isEmpty(fxjProCostCount.getId())){
					//外键设置
					fxjProCostCount.setProNo(fxjProMain.getProNo());
					this.save(fxjProCostCount);
				}
			}
		}
	}

	public void delMain(FxjProMainEntity fxjProMain) throws Exception{
		//删除主表信息
		this.delete(fxjProMain);
		//===================================================================================
		//获取参数
		Object proNo0 = fxjProMain.getProNo();
		Object proNo1 = fxjProMain.getProNo();
		Object proNo2 = fxjProMain.getProNo();
		Object proNo3 = fxjProMain.getProNo();
		Object proNo4 = fxjProMain.getProNo();
		//===================================================================================
		//删除-附件
	    String hql0 = "from FxjProAttEntity where 1 = 1 AND proNo = ? ";
	    List<FxjProAttEntity> fxjProAttOldList = this.findHql(hql0,proNo0);
		this.deleteAllEntitie(fxjProAttOldList);
		//===================================================================================
		//删除-合同
	    String hql1 = "from FxjProContractEntity where 1 = 1 AND proNo = ? ";
	    List<FxjProContractEntity> fxjProContractOldList = this.findHql(hql1,proNo1);
		this.deleteAllEntitie(fxjProContractOldList);
		//===================================================================================
		//删除-GPS
	    String hql2 = "from FxjProGpsEntity where 1 = 1 AND proNo = ? ";
	    List<FxjProGpsEntity> fxjProGpsOldList = this.findHql(hql2,proNo2);
		this.deleteAllEntitie(fxjProGpsOldList);
		//===================================================================================
		//删除-费用
	    String hql3 = "from FxjProCostEntity where 1 = 1 AND proNo = ? ";
	    List<FxjProCostEntity> fxjProCostOldList = this.findHql(hql3,proNo3);
		this.deleteAllEntitie(fxjProCostOldList);
		//===================================================================================
		//删除-其他计算
	    String hql4 = "from FxjProCostCountEntity where 1 = 1 AND proNo = ? ";
	    List<FxjProCostCountEntity> fxjProCostCountOldList = this.findHql(hql4,proNo4);
		this.deleteAllEntitie(fxjProCostCountOldList);
	}
 	
}